package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;


    /**
     * 新增线索跟进
     * @param tbClueTrackRecord
     * @return
     */
    @Override
    public int save(ClueTrackRecordVo tbClueTrackRecord) {
        //获取当前使用的用户名字
        String username = SecurityUtils.getUsername();
        Map<String, Object> map = new HashMap<>();
        map.put("createBy", username);//将用户名字存进集合
        TbClueTrackRecord tbClueTrackRecord1 = new TbClueTrackRecord();
        org.springframework.beans.BeanUtils.copyProperties(tbClueTrackRecord, tbClueTrackRecord1);//调用copy方法进行数据粘贴。
        tbClueTrackRecord1.setParams(map);
        int row = tbClueTrackRecordMapper.save(tbClueTrackRecord1);//以上主要是用来进行用户不是从线索池来的进行的操作

        TbClue tbClue = new TbClue();//线索
        tbClue.setId(tbClueTrackRecord.getClueId());//设置线索id
        tbClue.setName(tbClueTrackRecord.getName());//设置客户名字
        tbClue.setLevel(tbClueTrackRecord.getLevel());//设置意向等级
        tbClue.setSubject(tbClueTrackRecord.getSubject());//设置意向学科
        tbClue.setUpdateTime(new Date());//设置修改时间为当前时间
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());//设置状态
        int low = tbClueMapper.updateTbClue(tbClue);
        if (row<0&&low<0){
        return 0;}
        int hang = row>low?row:low;
        return hang;

    }

    /**
     * 根据线索id回显
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> all(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.all(clueId);
        return list;
    }


}
